package com.yingxin.yxerp.module.report.task.request.controller;

import com.yingxin.yx.framework.commons.enums.task.DynamicTaskLogCategoryEnum;
import com.yingxin.yx.framework.commons.enums.task.DynamicTaskName;
import com.yingxin.yx.framework.commons.enums.task.DynamicTaskTypeEnums;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.vo.KeyValueRespVo;
import com.yingxin.yxerp.module.report.entity.enums.DynamicTaskLogStatusEnums;
import com.yingxin.yxerp.module.report.task.request.dto.top.Dto;
import com.yingxin.yxerp.module.report.task.request.service.InterfaceService;
import com.yingxin.yxerp.module.report.task.request.vo.req.DynamicTaskRequestQueryVo;
import com.yingxin.yxerp.module.report.task.request.vo.req.DynamicTaskRequestUpdateVo;
import com.yingxin.yxerp.module.report.task.request.vo.resp.DynamicTaskRequestRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 动态定时任务管理 前端控制器
 * @author yuebao
 * @since 2023-12-07
 */

@Api(tags = "动态定时任务管理")
@RestController
@RequestMapping("/dynamic/task/request")
public class DynamicTaskRequestController {

    @Autowired
    private InterfaceService interfaceService;

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<DynamicTaskRequestRespVo> page(@RequestBody DynamicTaskRequestQueryVo queryVo) {
        try{
            return interfaceService.list( queryVo );
        }catch (Exception e){
            throw new BizException("请检查任务服务器是否正常启动！");
        }
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public DynamicTaskRequestRespVo get(@PathVariable("id") String id) {
        return interfaceService.getVo( id );
    }

    @ApiOperation(value = "联机")
    @GetMapping("/online/{id}")
    public Dto online(@PathVariable("id") String id) {
        return interfaceService.online( id );
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public Dto update(@RequestBody DynamicTaskRequestUpdateVo updateVo) {
        return interfaceService.update( updateVo );
    }

    @ApiOperation(value = "执行一次")
    @GetMapping("/run/{id}")
    public Dto run(@PathVariable("id") String id) {
        return interfaceService.run( id );
    }

    @ApiOperation(value = "启动或停止")
    @PostMapping("/startorstop")
    public Dto startOrStop(@RequestBody DynamicTaskRequestUpdateVo updateVo) {
        return interfaceService.StartOrStop ( updateVo.getId(), updateVo.getStatus() );
    }

    @ApiOperation(value = "删除任务")
    @GetMapping("/delete/{id}")
    public Dto delete(@PathVariable("id") String id) {
        return interfaceService.delete( id );
    }

    @ApiOperation(value = "获取来源列表")
    @PostMapping("/category/list")
    public List<KeyValueRespVo> categoryList() {
        // return interfaceService.getCategoryList();
        DynamicTaskLogCategoryEnum[] enums = DynamicTaskLogCategoryEnum.values();
        List<KeyValueRespVo> vos = new ArrayList<>();
        for( DynamicTaskLogCategoryEnum e : enums ){
            KeyValueRespVo vo = new KeyValueRespVo();
            vo.setKey( e.getType() );
            vo.setValue( e.getTypeName() );

            vos.add( vo );
        }
        return vos;
    }

    @ApiOperation(value = "获取任务分类列表")
    @PostMapping("/task/type/list")
    public List<KeyValueRespVo> taskTypeList() {
        // return interfaceService.getTaskTypeList();
        DynamicTaskTypeEnums[] enums = DynamicTaskTypeEnums.values();
        List< KeyValueRespVo > vos = new ArrayList<>();
        for( DynamicTaskTypeEnums e : enums ){
            KeyValueRespVo vo = new KeyValueRespVo();
            vo.setKey( e.getType() );
            vo.setValue( e.getTypeName() );

            vos.add( vo );
        }
        return vos;
    }

    @ApiOperation(value = "任务名称列表")
    @PostMapping("/task/name/list")
    public List<KeyValueRespVo> taskNameList() {
        // return interfaceService.getTaskNameList();
        DynamicTaskName[] enums = DynamicTaskName.values();
        List< KeyValueRespVo > vos = new ArrayList<>();
        for( DynamicTaskName e : enums ){
            KeyValueRespVo vo = new KeyValueRespVo();
            vo.setKey( e.getType() );
            vo.setValue( e.getTypeName() );

            vos.add( vo );
        }
        return vos;
    }

    @ApiOperation(value = "任务状态列表")
    @PostMapping("/task/status/list")
    public List<KeyValueRespVo> taskStatusList() {
        // return interfaceService.getStatusList();
        DynamicTaskLogStatusEnums[] enums = DynamicTaskLogStatusEnums.values();
        List< KeyValueRespVo > vos = new ArrayList<>();
        for( DynamicTaskLogStatusEnums e : enums ){
            KeyValueRespVo vo = new KeyValueRespVo();
            vo.setKey( e.getStatus().toString() );
            vo.setValue( e.getStatusName() );

            vos.add( vo );
        }
        return vos;
    }

}
